package com.javaee.controller.chat;

import com.javaee.BeforeBaseController;
import com.javaee.entity.Doctor;
import com.javaee.entity.User;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

@Controller
public class ChatController extends BeforeBaseController {
    @RequestMapping("/toChat")
    public String toChat(HttpSession session){
        if(session.getAttribute("User")!=null) {
            User user = (User) session.getAttribute("User");
            session.setAttribute("user","患者:"+user.getName());
        } else if (session.getAttribute("Doctor")!=null) {
            Doctor doctor = (Doctor) session.getAttribute("Doctor");
            session.setAttribute("user","医生:"+doctor.getName());
        } else  {
            String username = null;
            Random random = new Random();
            username = "游客" + random.nextInt(1000);
            session.setAttribute("user",username);
        }
        return "chat/main";
    }
    @GetMapping("/getUsername")
    @ResponseBody
    public String getUsername(HttpSession session) {
        String username = (String) session.getAttribute("user");
        if (username == null) {

            return username;
        } else {
            return username;
        }
    }
}
